<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Register extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        $data = array();

        $this->load->helper('email');
        
        if ($this->input->post('email') && valid_email($this->input->post('email'))) {

        	$this->load->helper('recaptcha');
        	
        	$resp = recaptcha_check_answer(
        				RECAPTCHA_PRIVATE_KEY,
        				$_SERVER['REMOTE_ADDR'],
        				$this->input->post('recaptcha_challenge_field'),
        				$this->input->post('recaptcha_response_field') 
        			);
        			
        	if($resp->is_valid == FALSE) {

        		redirect(site_url('memberlogin/index') . '?rv=' . $this->input->post('referral_uid', TRUE) . '&try=1');
        		
        	}
        	
        	$this->load->model("register_model");
        	
        	$isRegister = $this->register_model->checkMemberByEmail($this->input->post('email', TRUE));
        	
        	if($isRegister > 0) {
        		
        		redirect(site_url('memberlogin/index') . '?rv=' . $this->input->post('referral_uid', TRUE) . '&try=3');
        		
        	}
        	
            $user_data = array();
            
            $user_data['email'] 		= $this->input->post("email", TRUE);
            $user_data['value'] 		= BASE_PRICE;
            $user_data['referral_uid']	= $this->input->post("referral_uid", TRUE);
            
            $add = $this->register_model->addUserRegister($user_data);
            
            if($add == FALSE) {
            	
            	redirect(site_url('memberlogin/index') . '?rv=' . $this->input->post('referral_uid', TRUE) . '&try=4');
            	
            }
            
            $this->load->library('shorten');
            
            $token = $this->shorten->run($add, FALSE, 4, 'g1p');
            
            $this->load->library('email');
            
			$mailMsg = "Dear Valued Customer \n Silahkan untuk mentransfer senilai Rp. " . (BASE_PRICE + $add) . '. lalu klik link untuk konfirmasi. ' . base_url() . "register/confirm/" . $token;
          //error_log($mailMsg . "\n", 3, '/tmp/mail');
            $this->email->from('cbcsystem@yahoo.com', 'CBC System');
            $this->email->to($this->input->post("email"));
            $this->email->subject('CBC Email Register.');
            $this->email->message($mailMsg);
            $this->email->send();
            //echo $this->email->print_debugger();
            $data['info'] = $this->input->post("email", TRUE);
            $data['template']['content'] = "register/index";
            $this->load->view('essential/template', $data);
        } else {
            redirect(site_url('memberlogin/index') . '?rv=' . $this->input->post('referral_uid', TRUE) . '&try=2');
        }
    }

    public function confirm($token = false) {
        $data = array();
        if ($token) {
        	
        	$this->load->library('shorten');
        	$this->load->model("register_model");
            
            $incId	= $this->shorten->run($token, TRUE, 4, 'g1p');
            $check	= $this->register_model->getRegUser($incId, 0);

            if ($check) {
                
            	if($this->input->post('submit')) {
            		
            		$update = $this->register_model->updateConfirm($incId);
            		
            		if($update == TRUE) {
            			
            			$data['info'] = 'success';
            			
            		} else {
            			
            			$data['info'] = 'error';
            			
            		}
            		
            	} else {
            		
            		$data['info'] = 'terms';
            	}

            } else {
                $data['info'] = "invalid";
            }
        } else {
            $data['info'] = "invalid";
            
        }
        
        $data['template']['content'] = "register/thanks";
        $this->load->view('essential/template', $data);
    }

    public function success($uid = false) {
        $this->load->model("register_model");
        $check = $this->register_model->checkMember($uid);
        $data = array();
        if ($check) {
            //$this->register_model->activeMember($uid);
            $data['info'] = 'silahkan untuk mengklik link untuk login. ' . base_url() . 'loginMember';
        } else {
            $data['info'] = "link tidak valid.";
        }
        $data['template']['content'] = "register/thanks";
        $this->load->view('essential/template', $data);
    }

}